新建一個class extends SQLiteOpener
getWritableDatabase function 可以打開資料庫
public class MySQLiteHelper extends SQLiteOpenHelper {
// public static final String TABLE_COMMENTS = "comments";
// public static final String COLUMN_ID = "_id";
// public static final String COLUMN_COMMENT = "comment";
//
// private static final String DATABASE_NAME = "commments.db";
//
private static final int DATABASE_VERSION = 1;
public MySQLiteHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version) {
super(context, name,factory,version);
//上下文 資料庫名 cursor 版本號
mContext=context;
}
// Database creation sql statement
private static final String CREATE_BOOK = "create table BOOK"
+ "("
+ " id integer primary key autoincrement, " + "author text,"
+"price real,"
+"pages integer,"
+"name text);";
private static final String CREATE_CATEGORY = "create table Category"
+ "("
+ " id integer primary key autoincrement, " + "category text,"
+"category_code integer);";
private Context mContext;
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_BOOK);
database.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"sussed",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS BOOK" );
db.execSQL("DROP TABLE IF EXISTS Category" );
onCreate(db);
}
}
public class MainActivity extends AppCompatActivity {
private MySQLiteHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MySQLiteHelper(this, "BookStore.db", null, 3);
Button create=(Button) findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
Button add=(Button) findViewById(R.id.add);
Button update=(Button) findViewById(R.id.up);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price",11);
database.update(
"Book",values,"name=?",new String[]{
"uuko"
});
}
});
Button del =(Button)findViewById(R.id.del);
Button query=(Button) findViewById(R.id.query);
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase database=dbHelper.getWritableDatabase();
Cursor cursor=database.query("Book",
null,null,null,
null,null,null);
//表 列 where條件 具體值 groupby having orderby
if (cursor.moveToFirst()){
do {
String name=cursor.getString(cursor.getColumnIndex("name"));
Log.d("MainActivity", "onClick: "+name);
}while (cursor.moveToNext());
}
cursor.close();
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
database.delete("Book","pages>?",new String[] {"500"});
}
});
//
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
//組裝數據
values.put("name","uuko");
values.put("author","yy");
values.put("pages",444);
values.put("price",10.55);
database.insert("Book",null,values);
values.put("name","ttt");
values.put("author","tt");
values.put("pages",41);
values.put("price",3.55);
database.insert("Book",null,values);
}
});
}
}